﻿using System;

namespace CG5_KT.Utils
{
    public class FPSCalc
    {
        private long lastTick;
        private int lastFrameRate;
        private int frameRate;

        public delegate void FPSChangedEventHandler(int newFPS);
        public event FPSChangedEventHandler FPSChanged;

        public int CalculateFrameRate()
        {

            // DateTime.Now.Ticks represents the number of 100-nanosecond intervals
            // that have elapsed since 12:00:00 midnight, January 1, 0001.
            // 10000000 corresponds to one second.
            if (DateTime.Now.Ticks - lastTick >= 10000000)
            {
                lastFrameRate = frameRate;
                FPSChanged(lastFrameRate);

                frameRate = 0;
                lastTick = DateTime.Now.Ticks;
            }

            frameRate++;

            return lastFrameRate;
        }
    }
}
